<template>
  <!-- 挤压几何体/拉伸体（ExtrudeGeometry）,可以使用一个二维图形创建出一个三维图形-->
  <div class="shape-geometry">
    <div id="scene"></div>
    <div class="controls-box">
      <section>
        <el-row>
          <div v-for="(item, key) in properties" :key="key">
            <div v-if="item && item.name != undefined">
              <el-col :span="8">
                <span class="vertice-span">{{ item.name }}</span>
              </el-col>
              <el-col :span="13">
                <el-slider
                  v-model="item.value"
                  :min="item.min"
                  :max="item.max"
                  :step="item.step"
                  @change="redraw"
                ></el-slider>
              </el-col>
              <el-col :span="3">
                <span class="vertice-span">{{ item.value }}</span>
              </el-col>
            </div>
          </div>
        </el-row>
        <el-row>
          <el-checkbox v-model="properties.bevelEnabled" @change="redraw"
            >bevelEnabled</el-checkbox
          >
        </el-row>
      </section>
    </div>
  </div>
</template>

<script>
import * as THREE from "three";
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js";

let scene;
let camera;
let renderer;
let controls;
export default {
  data() {
    return {
      // 通过depth属性调整几何体拉伸的高度
      // 通过bevelThickness属性指定斜角的厚度
      // 通过bevelSize属性指定斜角的高度
      // 通过bevelSegments属性指定斜角的分段数
      // 通过bevelEnabled属性设置是否开启斜角
      // 通过curveSegments属性指定拉伸曲线的分段数
      // 通过steps属性指定拉伸体的分段数
      properties: {
        depth: {
          name: "depth",
          value: 2,
          min: 0,
          max: 40,
          step: 1,
        },
        bevelThickness: {
          name: "bevelThickness",
          value: 2,
          min: 0,
          max: 10,
          step: 0.1,
        },
        bevelSize: {
          name: "bevelSize",
          value: 0.8,
          min: 0,
          max: 10,
          step: 0.1,
        },
        bevelSegments: {
          name: "bevelSegments",
          value: 3,
          min: 0,
          max: 40,
          step: 1,
        },
        curveSegments: {
          name: "curveSegments",
          value: 12,
          min: 0,
          max: 40,
          step: 1,
        },
        steps: {
          name: "steps",
          value: 1,
          min: 0,
          max: 5,
          step: 1,
        },
        bevelEnabled: true,
      },
      mesh: null,
    };
  },
  mounted() {
    this.init();
  },
  methods: {
    // 初始化
    init() {
      this.createScene(); // 创建场景
      this.createMesh(); // 创建网格模型
      this.createLight(); // 创建光源
      this.createCamera(); // 创建相机
      this.createRender(); // 创建渲染器
      this.createControls(); // 创建控件对象
      this.render(); // 渲染
    },
    // 创建场景
    createScene() {
      scene = new THREE.Scene();
      //红色代表 X 轴. 绿色代表 Y 轴. 蓝色代表 Z 轴.
      scene.add(new THREE.AxesHelper(20));
    },
    drawShape() {
      // 创建基础形状
      const shape = new THREE.Shape();

      // 设置起始点
      shape.moveTo(-10, 0);
      // 画线到新坐标
      shape.lineTo(-10, 30);

      // 贝塞尔曲线
      shape.bezierCurveTo(-5, 20, 5, 20, 10, 30);
      shape.lineTo(10, 0);

      // 左眼
      const hole1 = new THREE.Path();
      hole1.absellipse(-5, 18, 2.3, 0.7, 0, Math.PI * 2, true);
      shape.holes.push(hole1);

      // 右眼
      const hole2 = new THREE.Path();
      hole2.absellipse(5, 18, 2.3, 0.7, 0, Math.PI * 2, true);
      shape.holes.push(hole2);

      // 鼻子
      const hole3 = new THREE.Path();
      hole3.moveTo(0, 14);
      hole3.lineTo(1, 12);
      hole3.lineTo(-1, 12);
      shape.holes.push(hole3);

      // 嘴巴
      const hole4 = new THREE.Path();
      hole4.absellipse(0, 6, 5, 1, 0, Math.PI * 2, true);
      shape.holes.push(hole4);

      shape.lineTo(-10, 0);

      return shape;
    },
    // 创建网格模型
    createMesh() {
      // 拉伸几何体配置项
      const options = {
        depth: this.properties.depth.value,
        bevelThickness: this.properties.bevelThickness.value,
        bevelSize: this.properties.bevelSize.value,
        bevelSegments: this.properties.bevelSegments.value,
        bevelEnabled: this.properties.bevelEnabled,
        curveSegments: this.properties.curveSegments.value,
        steps: this.properties.steps.value,
      };

      // 创建拉伸几何体
      const geom = new THREE.ExtrudeGeometry(this.drawShape(), options);
      geom.applyMatrix4(new THREE.Matrix4().makeTranslation(0, -10, 0));
      // 创建材质
      const meshMaterial = new THREE.MeshNormalMaterial({
        flatShading: false,
        transparent: true,
        opacity: 0.5,
      });

      // 创建网格对象
      this.mesh = new THREE.Mesh(geom, meshMaterial);
      // 网格对象添加到场景中
      scene.add(this.mesh);
    },
    // 创建光源
    createLight() {
      const ambientLight = new THREE.AmbientLight(0xffffff, 0.1); // 创建环境光
      scene.add(ambientLight); // 将环境光添加到场景

      const spotLight = new THREE.PointLight(0xffffff); // 创建聚光灯
      spotLight.position.set(0, 10, 20);
      spotLight.castShadow = true;
      scene.add(spotLight);
    },
    // 创建相机
    createCamera() {
      const element = document.getElementById("scene");
      const k = element.clientWidth / element.clientHeight; // 窗口宽高比
      camera = new THREE.PerspectiveCamera(35, k, 0.1, 1000);
      camera.position.set(0, 0, 80); // 设置相机位置

      scene.add(camera);
    },
    // 创建渲染器
    createRender() {
      const element = document.getElementById("scene");
      renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true });
      renderer.setSize(element.clientWidth, element.clientHeight); // 设置渲染区域尺寸
      renderer.shadowMap.enabled = true; // 显示阴影
      renderer.shadowMap.type = THREE.PCFSoftShadowMap;
      renderer.setClearColor(0x3f3f3f, 1); // 设置背景颜色
      element.appendChild(renderer.domElement);
    },
    // 重绘
    redraw() {
      scene.remove(this.mesh);
      this.createMesh();
    },
    render() {
      renderer.render(scene, camera);
      requestAnimationFrame(this.render);
    },
    // 创建控件对象
    createControls() {
      controls = new OrbitControls(camera, renderer.domElement);
    },
  },
};
</script>
<style>
.shape-geometry {
  width: 100%;
  height: 100%;
  position: relative;
}
#scene {
  width: 100%;
  height: 100%;
}
.controls-box {
  position: absolute;
  right: 5px;
  top: 5px;
  width: 300px;
  padding: 10px;
  background-color: #fff;
  border: 1px solid #c3c3c3;
}
</style>

